package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.userInfo.entity.Article;
import com.rongshu.view.userInfo.listener.OnItemFunctionClickListener;
import com.widget.SwipeMenuView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SerializeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;

    private List<Article.ResponseBean> articles;

    //interface
    private OnItemFunctionClickListener listener = null;

    public SerializeAdapter(Context context) {
        this.mContext = context;
    }

    public void setArticles(List<Article.ResponseBean> list) {
        this.articles = list;
    }

    public void addArticles(List<Article.ResponseBean> list) {
        this.articles.addAll(list);
    }

    public List<Article.ResponseBean> getArticles() {
        return this.articles;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_serizlize_item_view, parent, false);
        return new SerializeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        final Article.ResponseBean article = articles.get(position);
        final SerializeViewHolder holder = (SerializeViewHolder) viewHolder;
        //title
        holder.txt_article.setText(article.getTitle());
        //sum
        holder.txt_article_number.setText(article.getCount() + "");
        //listener
        holder.layout_article.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(position, article.getId(), article.getTitle());
                }
            }
        });
        holder.btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onEditClick(position, article.getId(), article.getTitle());
                    holder.swipemenu_view.quickClose();
                }
            }
        });
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDeleteClick(position, article.getCount(), article.getId());
                    holder.swipemenu_view.quickClose();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return articles == null ? 0 : articles.size();
    }

    public static class SerializeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.layout_article)
        RelativeLayout layout_article;
        @BindView(R.id.txt_article)
        TextView txt_article;
        @BindView(R.id.txt_article_number)
        TextView txt_article_number;
        @BindView(R.id.swipemenu_view)
        SwipeMenuView swipemenu_view;
        @BindView(R.id.btn_edit)
        TextView btn_edit;
        @BindView(R.id.btn_delete)
        TextView btn_delete;

        public SerializeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemFunctionClickListener(OnItemFunctionClickListener listener) {
        this.listener = listener;
    }
}
